package com.leeoohoo.config;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDateTime locDateTime) {
        return (locDateTime == null ? null : new Date(locDateTime.getSecond()));
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Date sqlTimestamp) {
        return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDate().atTime(LocalTime.now()));
    }
}